//
//  ReadBookVOrH_VC+PopWords.m
//  KidReading
//
//  Created by telen on 16/3/8.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadBookVOrH_VC+PopWords.h"
#import "ConstDefine.h"

@interface ReadBookVOrH_VC()
@property(nonatomic,strong)MemoView* memoView;
@end

@implementation ReadBookVOrH_VC (MemoView)

ASSOCIATED(memoView, setMemoView, MemoView*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

ASSOCIATED(editBtns, setEditBtns, NSArray<UIButton*>*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

+ (void)load
{
    [self swizzleSelector:@selector(viewDidLoad) withSelector:@selector(viewDidLoad_memoView)];
    [self swizzleSelector:NSSelectorFromString(@"dealloc") withSelector:@selector(dealloc_MemoView)];
}

- (void)viewDidLoad_memoView
{
    [self viewDidLoad_memoView];
    //
    //
    [mutex addMutexView:self.memoView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickedMemoBtn:) name:MemoButtonEvent_TouchUpInSile object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addMemoBtn:) name:MemoButtonEvent_AddNewMemoBtn object:nil];
}

- (void)dealloc_MemoView
{
    [self dealloc_MemoView];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MemoButtonEvent_TouchUpInSile object:nil];
}
     
- (void)clickedMemoBtn:(NSNotification*)noti
{
    NSDictionary* dict = [noti userInfo];
    MemoButton* mbtn = dict[@"mbtn"];
    self.memoView.pt_unshowFollowView = mbtn;
    [self.memoView setText:mbtn.txt];
    if (self.memoView.hidden == YES) {
        [self.memoView show];
        [self editBtns_update];
    }
}

- (void)addMemoBtn:(NSNotification*)noti
{
    NSDictionary* dict = [noti userInfo];
    MemoButton* mbtn = dict[@"mbtn"];
    self.memoView.pt_unshowFollowView = mbtn;
    [self.memoView setText:mbtn.txt];
    if (self.memoView.hidden == YES) {
        [self.memoView show];
        [self editBtns_update];
        if(readView.currentPageView.memoView.editModel)[self.memoView textBecomeFirstResponder];
    }
}

- (void)editBtns_update
{
    if (!self.editBtns) {
        CGFloat ratio = ratio_w;
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            ratio /= 1.28;
        }
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"btn_trash_normal.png"] forState:UIControlStateNormal];
        [btn addTouchScaleTransitions];
        btn.frame = CGRectMake(0, 0, 44/ratio, 44/ratio);
        self.editBtns = @[btn];
        [btn addTarget:self action:@selector(clicked_deleteMemo) forControlEvents:UIControlEventTouchUpInside];
    }
    if(readView.currentPageView.memoView.editModel)self.memoView.topBtns = self.editBtns;
    else self.memoView.topBtns = nil;
}

- (void)clicked_deleteMemo
{
    [UIAlertView tl_showTitle:@"" message:ReadLocalizedString(@"msg_del_memo") didClick:^(UIAlertView * _Nullable alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [readView.currentPageView.memoView list_DelBtn:self.memoView.pt_unshowFollowView];
            [self.memoView unShow_must];
            [self.memoView.pt_unshowFollowView boom];
            [DataProcessing do_Main:^{
                [self.memoView.pt_unshowFollowView removeFromSuperview];
            } delay:0.3];
        }
    } buttonTitles:ReadLocalizedString(@"cancel"),ReadLocalizedString(@"sure"),nil];
}

- (void)addMemoView
{
    CGFloat words_w = 280/ratio_w;
    CGFloat words_h = 280/ratio_w;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        words_w *= 1.25;
        words_h *= 1.25;
    }
    CGFloat w = width_my;
    CGFloat h = height_my;
    CGPoint pt_show = CGPointMake(w*1/2, h*1/2);
    CGPoint pt_unshow = pt_show;
    //
    if (!self.memoView) {
        self.memoView = [[MemoView alloc] initWithFrame:CGRectMake(0, 0, words_w, words_h)];
        [self.view addSubview:self.memoView];
        self.memoView.center = pt_show;
    }
    self.memoView.pt_show = pt_show;
    self.memoView.pt_unshow = pt_unshow;
    self.memoView.pt_unshowFollowView = nil;
    self.memoView.readView = readView;
    self.memoView.transform = CGAffineTransformMakeScale(0, 0);
    self.memoView.hidden = YES;
    self.memoView.bookName = self.bookName;
    
    __weak ReadBookVOrH_VC* _wself = self;
    __weak ReadView* _wreadview = readView;
    self.memoView.textToBecomeFirstResponder = ^BOOL(MemoView* memoView){
        if (_wreadview.currentPageView.memoView.editModel) {
            InputViewController* messageVC = [[InputViewController alloc] init];
            messageVC.tapEmptyType = TapEmptyType_Finish;
            [messageVC showMessageVCOnParentVC:_wself OriginalMessage:memoView.text MessageDone:^(NSString *messageStr) {
                [memoView updateText:messageStr];
            }];
        }
        return YES;
    };
}

- (NSNumber*)didTapSelfView_unShowMemoView
{
    if (self.memoView && !self.memoView.hidden) {
        [self.memoView unShow];
        return @(YES);
    }
    return nil;
}

- (NSNumber*)isMemoViewShowing
{
    if(self.memoView.isInShowing){
        return @(YES);
    }
    return nil;
}

- (NSNumber*)isMemoViewWorking
{
    if(self.memoView.hidden == NO || self.memoView.isInShowing){
        return @(YES);
    }
    return nil;
}

- (void)memoViewMustUnshow
{
    [self.memoView unShow_must];
}

@end
